package com.itcast.activiti.controller;


import cn.itcast.ic.common.bean.Response;
import com.itcast.activiti.entity.ActIdPassUser;
import com.itcast.activiti.entity.ActIdUser;
import com.itcast.activiti.entity.ApproveField;
import com.itcast.activiti.service.IActIdPassUserService;
import com.itcast.activiti.service.IActIdUserService;
import com.itcast.activiti.utils.ActivitiPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author itcast
 * @since 2020-12-31
 */
@RestController
@RequestMapping("/user")
public class ActIdUserController {
    @Autowired
    private IActIdUserService iActIdUserService;
    @Autowired
    private IActIdPassUserService iActIdPassUserService;

    @GetMapping(path = "/{email}")
    public Response getUserName(@PathVariable("email") String email) {
        return Response.success(iActIdUserService.getUserNameByemail(email));
    }

    @PostMapping(path = "/list")
    public Response listActIdUserPage(@RequestBody ActivitiPage activitiPage) {
        return Response.success(iActIdUserService.listActIdUserPage(activitiPage));
    }

    @PostMapping(value = "/add")
    public Response addApproveField(@RequestBody ActIdUser actIdUser) {
        return Response.success(iActIdUserService.add(actIdUser));
    }

    @DeleteMapping(path = "/del/{id}")
    public Response delApproveField(@PathVariable("id") Integer id, @RequestParam(required = false) String userId) {
        return Response.success(iActIdUserService.del(id, userId));
    }

    /**
     * 查询指定工作流用户
     */
    @GetMapping("/process/{actIdProcessId}")
    public Response listActIdPassUser(@PathVariable Integer actIdProcessId, @RequestParam(required = false) String userEmail) {
        return Response.success(iActIdPassUserService.listActIdPassUserByActIdProcess(actIdProcessId, userEmail));
    }

    /**
     * 指定工作流用户-添加/编辑
     */
    @PostMapping("/process/{actIdProcessId}")
    public Response addActIdPassUser(@PathVariable Integer actIdProcessId, @RequestBody Map<String, List> map, @RequestParam(required = false) String createdBy) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        List listUserEmail = map.get(ActIdPassUser.Fields.userEmail);
        listUserEmail.forEach(userEmail -> {
            List listPassUserEmail = map.get(ActIdPassUser.Fields.passUserEmail);
            Map<String, Object> map1 = new HashMap<>();
            map1.put(ActIdPassUser.Fields.userEmail, userEmail);
            map1.put(ActIdPassUser.Fields.passUserEmail, String.join(",", listPassUserEmail));
            mapList.add(map1);
        });
        return iActIdPassUserService.addActIdPassUser(actIdProcessId, mapList, createdBy);
    }

    @DeleteMapping(path = "/process/del/{id}")
    public Response delActIdPassUser(@PathVariable("id") Integer id, @RequestParam(required = false) String deletedBy) {
        return Response.success(iActIdPassUserService.delActIdPassUser(id, deletedBy));
    }
}

